<?php
/**
 * 缩略图
 *
 * $thumbnail = Thumbnail::getInstance(array(
 *      'src' => '/a.jpeg'
 * ));
 * $thumbnail->resizeByWidthHeightPercent(0.5, 0.5);
 * $thumbnail->save('b.jpeg');
 *
 *
 *
 *
 *
 */

class Thumbnail {
    /** @var string 图片地址 */
    protected $src = '';
    /** @var int 图片宽度，像素 */
    protected $width = 0;
    /** @var int 图片高度，像素 */
    protected $height = 0;
    /** @var string 图片类型,如：gif，jpeg等 */
    protected $typeName = '';
    /** @var array 初始化的图片数据 */
    protected $imageInfo = array();
    /** @var null 实例 */
    protected static $instance = null;
    /** @var null  */
    protected $_img_source = null;
    /** @var int 文件大小，单位：字节 */
    protected $fileSize = 0;
    /** @var array 图片类型 */
    protected $types = array(
        1 => 'gif', 2 => 'jpeg', 3 => 'png', 6 => 'bmp', 15 => 'wbmp',
    );
    /** @var array 缩略的设置信息 */
    protected $thumbInfo = array();

    public function __construct($imageInfo = array()) {
        if(!$imageInfo['src']) throw new Exception(__CLASS__ . ' : $imageInfo,$imageInfo[\'src\'] empty', '9999');
        $this->src = $imageInfo['src'];
        list($this->width, $this->height, $type, $info) = getimagesize($this->src);
        $this->typeName = $this->types[$type];
        $this->fileSize = filesize($this->src);

        $imagecreatefunc = 'imagecreatefrom' . $this->typeName;
        $this->_img_source = $imagecreatefunc($this->src);

        $this->thumbInfo = array(
            'width' => $this->width,
            'height' => $this->height,
            'typeName' => $this->typeName,
            'src' => $this->src,
            'isResize' => 0, //是否要设置缩略
            '_img_source' => null
        );
    }

    /**
     * @param array $imageInfo array('src' => '')
     * @return null|Thumbnail
     */
    public static function getInstance($imageInfo = array()) {
        if(!self::$instance) self::$instance = new Thumbnail($imageInfo);

        return self::$instance;
    }

    /**
     * @param array $imageInfo array('src' => '')
     * @return null|Thumbnail
     */
    public static function getNewInstance($imageInfo = array()) {
        return self::$instance = new Thumbnail($imageInfo);
    }

    /**
     * 获取图片路径
     */
    public function getSrc() {
        return $this->src;
    }

    public function getWidth() {
        return $this->width;
    }

    public function getHeight() {
        return $this->height;
    }

    public function getTypeName() {
        return $this->typeName;
    }

    public function getFileSize() {
        return $this->fileSize;
    }

    /**
     * 根据固定的宽高缩略
     * @param $width
     * @param $height
     */
    public function resizeByWidthHeight($width, $height) {
        $this->thumbInfo['isResize'] = 1;
        $this->thumbInfo['width'] = $width;
        $this->thumbInfo['height'] = $height;
        $this->thumbInfo['_img_source'] = imagecreatetruecolor($this->thumbInfo['width'], $this->thumbInfo['height']);
        imagecopyresampled(
            $this->thumbInfo['_img_source'],
            $this->_img_source,
            0, 0, 0, 0,
            $this->thumbInfo['width'], $this->thumbInfo['height'],
            $this->width, $this->height
        );
    }

    /**
     * 根据宽高百分比缩略
     * @param $widthPercent = 0.5
     * @param $heightPercent = 0.3
     */
    public function resizeByWidthHeightPercent($widthPercent, $heightPercent) {
        $this->thumbInfo['isResize'] = 1;
        $this->thumbInfo['width'] = $this->width * $widthPercent;
        $this->thumbInfo['height'] = $this->height * $heightPercent;
        $this->thumbInfo['_img_source'] = imagecreatetruecolor($this->thumbInfo['width'], $this->thumbInfo['height']);
        imagecopyresampled(
            $this->thumbInfo['_img_source'],
            $this->_img_source,
            0, 0, 0, 0,
            $this->thumbInfo['width'], $this->thumbInfo['height'],
            $this->width, $this->height
        );
    }

    /**
     * 保存图片
     * @param string $src 为空则替换原图片
     */
    public function save($src = '') {
        if($src) $this->thumbInfo['src'] = $src;
        $imagefunc = 'image' . $this->typeName;
        $imagefunc($this->thumbInfo['_img_source'], $this->thumbInfo['src']);
    }

    /**
     * 输出图片
     */
    public function render() {
        $imagefunc = 'image' . $this->typeName;
        header('Content-Type: ' . $this->typeName);
        if($this->thumbInfo['isResize']) {
            $imagefunc($this->thumbInfo['_img_source']);
        } else {
            $imagefunc($this->_img_source);
        }

    }
}
?>